﻿<#import "../../_layout.ftl" as layout>
${Html.s("pagetitle","编辑流程")}
<@layout._layout>
<style>
.main{
	margin:5px;
}
</style>
<input type="hidden" id="formvariables" name="formvariables" />
<div class="head">
    <div id="title" class="title">
        
    </div>
</div>
<div class="main">
    <ul class="mydetail">     
        <li>
            <label>审批内容:</label> <input type="hidden" id="hdvariables"/>
            <form id="content" style="border:1px solid #ccc"></form>
        </li>  
    </ul>
</div>
<div style="text-align:center;">
	<button id="submitFlow" class="btn1">保存</button>&nbsp;&nbsp;<button id="lunchFlow" class="btn1">发起流程</button>
</div>
<script type="text/javascript">
	//获取url中的参数
    function getUrlParam(name) {
    	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }
	$(document).ready(function(){
		$.ajax({
			url:"flow_detail",
			type:"POST",
			dataType:"json",
			data:{
				flowid:getUrlParam("flowid")
			},
			success:function(data){
				$("#title").html("流程审批："+ data.data.flow.title);
				$("#content").html(data.data.model.pc_form_url);
				fillFormContent(data.data.flow.flow_variable_json);
				$("#formvariables").val(data.data.flow.flow_variable_json);			
			},
			error:function(){
			}			
		});
		$("#lunchFlow").click(function(){
			var data = new Array(); 
				$("#flowForm input,select,textarea").each(function(index){
					var input=new Object();			
					input.id=$(this).attr('id');
					input.name=$(this).attr('name');
					input.key=$(this).attr('name');
				    input.value=$(this).val();
				    input.type=$(this).attr('type');
				    input.hidden=$(this).attr('hidden');
				    if (input.type=='textarea'){
		                  input.value=$(this).text();
		              }else if (input.type=='combobox'){
		                  input.value=$(this).combobox('getValue');
		              }else if (input.type=='checkbox'){
		                  if ($(this).is(':checked')) 
								input.value=input.attr('xtext');
		              }else if (input.type!='button'){
			              input.value=$(this).val(); 
		              }	     		
					data.push(input);
				
				});		
			$.ajax({
				url:"saveAndlunchflow",
				type:"POST",
				dataType:"json",
				data:{
					flowid:getUrlParam("flowid"),					
					form:JSON.stringify(data)					
				},
				success:function(datas){						
					window.location.href="lunched_list";
				},
				error:function(e){
					alert(JSON.stringify(e));
				}
			})	
		});
		$("#submitFlow").click(function(){//提交流程
				var data = new Array(); 
				$("#flowForm input,select,textarea").each(function(index){
					var input=new Object();			
					input.id=$(this).attr('id');
					input.name=$(this).attr('name');
					input.key=$(this).attr('name');
				    input.value=$(this).val();
				    input.type=$(this).attr('type');
				    input.hidden=$(this).attr('hidden');
				    if (input.type=='textarea'){
		                  input.value=$(this).text();
		              }else if (input.type=='combobox'){
		                  input.value=$(this).combobox('getValue');
		              }else if (input.type=='checkbox'){
		                  if ($(this).is(':checked')) 
								input.value=input.attr('xtext');
		              }else if (input.type!='button'){
			              input.value=$(this).val(); 
		              }	     		
					data.push(input);
				
				});			
			$.ajax({
				url:"saveflow",
				type:"POST",
				dataType:"json",
				data:{
					flowid:getUrlParam("flowid"),					
					form:JSON.stringify(data)					
				},
				success:function(datas){						
					window.location.href="lunched_list";
				},
				error:function(e){
					alert(JSON.stringify(e));
				}
			})
		});
	});
	function fillFormContent(list)
	{
		var variables=JSON.parse(list);
		while(variables.length>0){
			var variable=variables.pop();
			console.log(variable.key);			
			if($("#"+variable.key))
			{				
				console.log(variable.value);		
				$("#"+variable.key).val(variable.value);
			}
		}
	}
	function show_detail(){
		fillFormContent(eval($("#hdvariables").val()));
	}
</script>

</@layout._layout>